package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Models.Documents;
import Models.InventoryUsage;
import Models.LabProcess;

public class LabProcessPresenter extends AbstractPresenter<LabProcess> {
	final String SINGLEITEM = "labProcess";
	final String MULTIITEM = "labProcesses";
	
	public ArrayList<LabProcess> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public LabProcess tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public LabProcess createItemFromJSONObject(JSONObject jsonObject){
		LabProcess item = new LabProcess();
		try{
			item.setLpID(jsonObject.getInt("lpid"));
			item.setMen(jsonObject.getInt("taskMen"));
			item.setMinutes(jsonObject.getInt("taskMinutes"));
			item.setRateType(jsonObject.getInt("rateType"));
			item.setLpDescription(jsonObject.getString("lpdesc"));
			//Set Documents Collection
			ArrayList<Documents> documents = new ArrayList<Documents>();
			if(!jsonObject.isNull("docCollection")){
				Object itm = jsonObject.get("docCollection");
				DocumentPresenter presenter = new DocumentPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						documents.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					documents.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setDocs(documents);
			}
			//Set Inventory Usage Collection
			ArrayList<InventoryUsage> invUsages = new ArrayList<InventoryUsage>();
			if(!jsonObject.isNull("invUsageCollection")){
				Object itm = jsonObject.get("invUsageCollection");
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						invUsages.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					invUsages.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setInventoryUsages(invUsages);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(LabProcess item){
		try{
			JSONObject json = new JSONObject();
			json.put("lpid", item.getLpID());
			json.put("taskMen", item.getMen());
			json.put("taskMinutes", item.getMinutes());
			json.put("rateType", item.getRateType());
			json.put("lpdesc", item.getLpDescription());
			//Create Doc Collection
			if(item.getDocs()!=null){
				DocumentPresenter presenter = new DocumentPresenter();
				if(item.getDocs().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getDocs().size(); i++){
						jsonArray.put(presenter.createJSON(item.getDocs().get(i)));
					}
					json.put("docCollection", jsonArray);
				}
			}
			//Create Doc Collection
			if(item.getInventoryUsages()!=null){
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(item.getInventoryUsages().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getInventoryUsages().size(); i++){
						jsonArray.put(presenter.createJSON(item.getInventoryUsages().get(i)));
					}
					json.put("invUsageCollection", jsonArray);
				}
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public LabProcess getEmptyEntity(){
		LabProcess entity = new LabProcess();
		return entity;
	}
}
